﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using FSY.Components.DataProvider;
using FSY.API.Utilities;
using FSY.API.CacheManager;
using FSY.Globals;
namespace FSY.Components.Entities
{
    public class Fsy_UserController
    {
        private static int InsertDeleteUpdate(Fsy_User user,DataProviderAction action)
        {
            Fsy_Cache.RemoveCache(cacheUserAll);
            Fsy_Cache.RemoveCache(cacheUserByRole);
            Fsy_Cache.RemoveCache(cacheUserByMenu);
            Fsy_Cache.RemoveCache(cacheUserByModule);
            return Fsy_DataProvider.Instance.User_AddDeleteUpdate(user,action);
        }
        public static int Insert(Fsy_User user)
        {
            return InsertDeleteUpdate(user, DataProviderAction.Insert);
        }
        public static int Delete(Fsy_User user)
        {
            return InsertDeleteUpdate(user, DataProviderAction.Delete);
        }
        public static int Update(Fsy_User user)
        {
            return InsertDeleteUpdate(user, DataProviderAction.Update);
        }

        private static object objlock = new object();
        private static string cacheUserAll = "allUser";
        public static List<Fsy_User> GetAllUser()
        {
            List<Fsy_User> lsUser = new List<Fsy_User>();
            lock (objlock)
            {
                lsUser = (List<Fsy_User>)Fsy_Cache.GetCache(cacheUserAll);
                if (lsUser == null)
                {
                    lsUser = Fsy_DataProvider.Instance.User_GetListAll();                   
                    if(lsUser != null)
                        Fsy_Cache.SetCache(cacheUserAll, lsUser, Fsy_Config.SlidingExpiration());
                    return lsUser;
                }
            }
            return lsUser;
        }

        public static Fsy_User GetUserByID(int userID)
        {
            List<Fsy_User> ls = GetAllUser();
            foreach (Fsy_User user in ls)
            {
                if (user.UserID == userID)
                {
                    return user;
                }
            }
            return null;
        }

        public static Fsy_User GetUserByUserName(string userName)
        {
            List<Fsy_User> ls = GetAllUser();
            foreach (Fsy_User user in ls)
            {
                if (user.UserName == userName)
                {
                    return user;
                }
            }
            return null;
        }

        public static Fsy_User GetUserByEmail(string email)
        {
            List<Fsy_User> ls = GetAllUser();
            foreach (Fsy_User user in ls)
            {
                if (user.Email == email)
                {
                    return user;
                }
            }
            return null;
        }

        private static string cacheUserByRole = "userByRole";
        public static List<Fsy_UserQuery> GetUserByRole(int role)
        {
            List<Fsy_UserQuery> lsUser = new List<Fsy_UserQuery>();
            lock (objlock)
            {
                lsUser = (List<Fsy_UserQuery>)Fsy_Cache.GetCache(cacheUserByRole);
                if (lsUser == null)
                {
                    lsUser = Fsy_DataProvider.Instance.User_GetByRole(role);
                    Fsy_Cache.SetCache(cacheUserByRole, lsUser, Fsy_Config.SlidingExpiration());
                    return lsUser;
                }
            }
            return lsUser;
        }

        private static string cacheUserByMenu = "userByMenu";
        public static List<Fsy_UserQuery> GetUserByMenu(int menu)
        {
            List<Fsy_UserQuery> lsUser = new List<Fsy_UserQuery>();
            lock (objlock)
            {
                lsUser = (List<Fsy_UserQuery>)Fsy_Cache.GetCache(cacheUserByMenu);
                if (lsUser == null)
                {
                    lsUser = Fsy_DataProvider.Instance.User_GetByMenu(menu);
                    Fsy_Cache.SetCache(cacheUserByMenu, lsUser, Fsy_Config.SlidingExpiration());
                    return lsUser;
                }
            }
            return lsUser;
        }

        private static string cacheUserByModule = "userByModule";
        public static List<Fsy_UserQuery> GetUserByModule(int module)
        {
            List<Fsy_UserQuery> lsUser = new List<Fsy_UserQuery>();
            lock (objlock)
            {
                lsUser = (List<Fsy_UserQuery>)Fsy_Cache.GetCache(cacheUserByModule);
                if (lsUser == null)
                {
                    lsUser = Fsy_DataProvider.Instance.User_GetByModule(module);
                    Fsy_Cache.SetCache(cacheUserByModule, lsUser, Fsy_Config.SlidingExpiration());
                    return lsUser;
                }
            }
            return lsUser;
        }

        /// <summary>
        /// Ham kiem tra user khi dang nhap
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>Null: User chua dang ky; Fsy_user</returns>
        public static Fsy_User CheckUser(string userName, string password)
        {
            try
            {
                List<Fsy_User> ls = GetAllUser();
                foreach (Fsy_User user in ls)
                {
                    if (user.UserName == userName && user.Password == Fsy_SecurityUtil.Encrypt(password, "fsy"))
                    {
                        return user;
                    }
                }
                return null;
            }
            catch(ArgumentNullException ex)
            {
                FsyLoggerHelper.WriteError("FSY.Components.Entities.Fsy_UserController -- CheckUser() ---" + ex.Message);
            }
            return null;
        }
    }
}
